var listPage = avalon.define({
	$id: "listPage",
	products: [],
	noProductData:"",
	noSolutionData:"",
	uid:"",
	solutions: []
});


mui.ready(function() {
	var p_pageNo = 1;
	var s_pageNo = 1;
	var mui_product=mui('#product-list');
	var mui_solution=mui('#solution-list');
	var uid=$("#uid").val();
	listPage.uid=uid;
	
	var btnArray = ['OK', 'Cancel'];
	btnArray[0]=$.i18n.prop('User_Center_ok');
	mui("#product-list").on('tap', '.mui-btn', function() {
		var id = this.getAttribute('data-id');
		mui.confirm('Confirm to delete this favourite？', 'Message', btnArray, function(e) {
			if (e.index == 0) {
				mui.ajax('/user/delFavorite.do', {
					data: {
						id: id,
						customerId: listPage.uid,
						type: 1
					},
					dataType: 'json',
					success: function(data) {
						if(data > 0){
							$("#"+id).remove();
						}
					}
				});
            } else {
            	
            }
		});
	});
	
	
	
	
	var mui_product = mui('#product-list');
	mui_product.pullRefresh({
		container: "#product-list",
		down: {
			height: 50,
			auto: true,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				listPage.noProductData="";
				setTimeout(function() {
					p_pageNo = 1;
					listPage.products = [];
					mui.ajax('/user/favoriteList.json', {
						data: {
							customerId: listPage.uid,
							type: 1,
							pageNo: p_pageNo
						},
						dataType: 'json',
						success: function(data) {
							listPage.products = data.products;
							mui_product.pullRefresh().endPulldownToRefresh();
							
							if(listPage.products.length==0){
								listPage.noProductData="<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
							
						}
					});
				}, window.DAHUA_CONFIG.LOADING_TIME);
			}
		},
		up: {
			height: 50,
			auto: false,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				listPage.noProductData="";
				setTimeout(function() {
					p_pageNo=p_pageNo+1;
					mui.ajax('/user/favoriteList.json', {
						data: {
							customerId: listPage.uid,
							type: 1,
							pageNo: p_pageNo
						},
						dataType: 'json',
						success: function(data) {
							var products=listPage.products;
							var items=data.products;
							avalon.each(items, function(i, item) {
								products.push(item);
							});
							listPage.products=products;
							mui_product.pullRefresh().endPullupToRefresh(false);
							
							if(listPage.products.length==0){
								listPage.noProductData="<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
						}
					});
				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});


	var mui_solution = mui('#solution-list');
	mui_solution.pullRefresh({
		container: "#solution-list",
		down: {
			height: 50,
			auto: true,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				listPage.noSolutionData="";
				setTimeout(function() {
					s_pageNo = 1;
					listPage.solutions = [];
					mui.ajax('/user/favoriteList.json', {
						data: {
							customerId: listPage.uid,
							type: 2,
							pageNo: s_pageNo
						},
						dataType: 'json',
						success: function(data) {
							listPage.solutions = data.solutions;
							mui_solution.pullRefresh().endPulldownToRefresh();
							
							if(listPage.solutions.length==0){
								listPage.noSolutionData="<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
							
						}
					});
				}, window.DAHUA_CONFIG.LOADING_TIME);
			}
		},
		up: {
			height: 50,
			auto: false,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				listPage.noSolutionData="";
				setTimeout(function() {
					s_pageNo=s_pageNo+1;
					mui.ajax('/user/favoriteList.json', {
						data: {
							customerId: listPage.uid,
							type: 2,
							pageNo: s_pageNo
						},
						dataType: 'json',
						success: function(data) {
							var solutions=listPage.solutions;
							var items=data.solutions;
							avalon.each(items, function(i, item) {
								solutions.push(item);
							});
							listPage.solutions=solutions;
							mui_solution.pullRefresh().endPullupToRefresh(false);
							
							if(listPage.solutions.length==0){
								listPage.noSolutionData="<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
							
						}
					});
				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});
	
	
	mui("#product-list").on('tap', 'li', function() {
		var pid = this.getAttribute('id');
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url+"?id="+pid
		});
	});

	mui("#solution-list").on('tap', 'img', function() {
		var sid = this.getAttribute('data-id');
		var url = "../solution/pdp.html";
		mui.openWindow({
			url: url+"?sid="+sid
		});
	});
	
	
	mui("#solution-list").on('tap', 'i', function() {
		var id = this.getAttribute('data-id');
		mui.confirm('Confirm to delete this favourite？', 'Message', btnArray, function(e) {
			if (e.index == 0) {
				mui.ajax('/user/delFavorite.do', {
					data: {
						id: id,
						customerId: listPage.uid,
						type: 2
					},
					dataType: 'json',
					success: function(data) {
						if(data > 0){
							$("#"+id).remove();
						}
					}
				});
            } else {
            	
            }
		});
	});

});